package com.atguigu.sso_client2.controller;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
public class Client2Controller {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("/client2/employee")
    public String employee(@RequestParam(value = "token",required = false) String token, HttpSession session){
        RestTemplate restTemplate=new RestTemplate();
        if(StringUtils.isEmpty(token)){
            return "redirect:http://ssoserver.com:8091/login.html?redirectUrl=http://ssoclient2.com:8093/client2/employee";
        }
        String url="http://ssoserver.com:8091/redisUserName?token={token}";
        Map<String,String> map=new HashMap<String, String>();
        map.put("token",token);
        ResponseEntity<String> forEntity = restTemplate.getForEntity(url, String.class, map);
        String userName=forEntity.getBody();
        session.setAttribute("username",userName);
        return "success";
    }

}
